package com.cykj.service.Impl;

import com.cykj.dto.ResponseDto;
import com.cykj.mapper.MenuMapper;
import com.cykj.mapper.RoleMapper;

import com.cykj.pojo.PsMenu;
import com.cykj.pojo.PsRole;

import com.cykj.service.RoleService;
import com.cykj.util.TreeMenu;

import com.cykj.vo.RoleMenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

/**
 * @BeLongsProject: psychologically
 * @BeLongsPackage: com.cykj.service.impl
 * @Author: 小李
 * @Date: 2025/2/6 00:29
 * @Description: 角色表service层
 * @Version: 1.0
 */
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private MenuMapper menuMapper;

    /**
     * 获取所有角色
     * @return
     */
    @Override
    public ResponseDto getAllRole() {
        List<PsRole> list = roleMapper.getAllRole();
        return ResponseDto.success(list);
    }

    //获取角色已分配菜单
    @Override
    public ResponseDto getRoleMenu(Integer roleId) {
        //1.已拥有的权限菜单
        List<PsMenu> listMenu =  roleMapper.getRoleMenu(roleId);
        //构建已拥有权限菜单的树形菜单
        List<PsMenu> toData =  TreeMenu.treeMenuList(listMenu);

        //2.数据库获取所有菜单信息
        List<PsMenu> allMenu = menuMapper.getAll();
        //全部菜单的树形结构
        List<PsMenu> fromData = TreeMenu.treeMenuList(allMenu);

        //3.过滤（所有菜单的树形结构，已有菜单）
        filterMenu(fromData, listMenu);

        //4.通过HashMap存储
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("toData", toData);//已分配的菜单
        hashMap.put("fromData", fromData); //全部菜单

        //将HashMap返回出去
        return ResponseDto.success(hashMap);
    }

    //修改菜单
    @Override
    public ResponseDto updateRoleMenu(RoleMenuVo vo) {
        //所有的菜单id
        List<Long> list =  new ArrayList<>();
        //获取所有菜单id
        for (PsMenu nextDatum : vo.getNextData()) {
            //添加菜单id
            list.add(nextDatum.getMenuId());
            //判断是否有子菜单
            if (nextDatum.getChildren().size()!=0){
                for (PsMenu child : nextDatum.getChildren()) {
                    list.add(child.getMenuId());
                }
            }
        }
        //1.删除该角色拥有的菜单
        Integer id =  roleMapper.deletaRoleMenu(vo.getRoleId());
        //2.更新该角色菜单
        if (list.size()>0){
            Integer id1 = roleMapper.addRoleMenu(list,vo.getRoleId());
        }
        return ResponseDto.success(null);
    }

    /**
     * 过滤菜单方法
     * @param fromData 全部菜单树形结构
     * @param listMenu 已拥有的菜单（普通结构）
     */
    private void filterMenu(List<PsMenu> fromData,List<PsMenu> listMenu) {
        //迭代器
        Iterator<PsMenu> iterator = fromData.iterator();
        //判断有无next元素
        while (iterator.hasNext()) {
            //获取当前对应的菜单（一级菜单）
            PsMenu menu = iterator.next();

            //1.判断是否有子菜单
            if(menu.getChildren().size()==0){
                //遍历已分配菜单
                for (PsMenu listMenu1 : listMenu) {
                      //判断菜单的id是否相等，相等就删除
                    if (listMenu1.getMenuId()==menu.getMenuId()){
                         iterator.remove();
                    }
                }
            }else {
                //2.有子菜单，则遍历子菜单（采用递归调用）
                filterMenu(menu.getChildren(),listMenu);
                //如果没有子菜单，则去掉父级菜单（过滤）
                if (menu.getChildren().size()==0){
                    iterator.remove();
                }
            }
        }
    }


}
